/**
 * Created with IntelliJ IDEA.
 * User: Dave Liu
 * Date: 15-10-21
 * Time: 下午8:34
 * To change this template use File | Settings | File Templates.
 */
(function($){
    var showwait = true;
    var beforeRender;//在渲染数据之前执行，接收到回调时立马执行。
    var dataDiv;
    //调用load方法需要传的参数
    var data;//查询参数包括分页参数{page:10,max:12}
    var url;//查询数据的地址
    var fields;//[{name:"id",autoHidden:true,text:"编号",inTable:true,render:function(){}},{name:"name",autoHidden:false,text:"名称",inTable:true}]
    var operations;//[{name:"remove",text:"删除",hadler:function(row){}},{name:"edit",text:"编辑",hadler:function(row){}}]
    var dialogTitle="";//对话框title
    var showDetail=true;
    var webContext;//网站根目录
    var async=true;
    var afterLoad;
    var loadSum;
    var traditional;//是否深度解析
    var renderBlock=false;
    var reportShowFields;
    var type="post";//请求类型
    var afterRenderEach;//在每次渲染数据之后执行。
    var showPages=true;
    var showContextMenuTip=true;

    //去后台查询后返回结果存到这里的
    var currentPage=0;
    var pages;
    var results;
    var rows;
    var offset=-1;
    var max=-1;
    var showFields;

    var scrollTopTemp=0;

    function renderDialog(i){
        if (typeof($("#dataDetail")) != 'undefined') {
            $("#dataDetail").remove();
        }
        row=rows[i];

        var rowParams={};
        var trClass="";
        var trStyle="";
        var trs="";
        for(j=0;j<fields.length;j++){
            var field=fields[j];
            //alert("field.name->"+field.name);
            var text=field.text;
            var value=row[field.name];
            //alert("value->"+value);
            if(field.isParam){
                var param=field.text;
                if(typeof(field.render)!='undefined'){
                    var index=i;
                    if(offset>-1){
                        index+=offset;
                    }
                    param=field.render(row,index,rowParams);
                }
                rowParams[field.name]=param;
                continue;
            }
            if(field.isTrClass){
                trClass=field.text;
                if(typeof(field.render)!='undefined'){
                    var index=i;
                    if(offset>-1){
                        index+=offset;
                    }
                    trClass=field.render(row,index,rowParams);
                }
                continue;
            }
            if(field.isTrStyle){
                trStyle=field.text;
                if(typeof(field.render)!='undefined'){
                    var index=i;
                    if(offset>-1){
                        index+=offset;
                    }
                    trStyle=field.render(row,index,rowParams);
                }
                continue;
            }
            if(typeof(field.inDetail)=="undefined"||field.inDetail==true) {
                trs += "<tr class='"+trClass+"' style='"+trStyle+"'>" ;
                trs += "   <td>" + text + "</td>";
                if (typeof(field.render) != 'undefined') {
                    var index=i;
                    if(offset>-1){
                        index+=offset;
                    }
                    value = field.render(row,index,rowParams);
                }
                if(value == null){
                    value = "";
                }
                trs += "   <td>" + value + "</td>";
                trs += "</tr>";
            }
        }


        var htmlStr=""+
            "<div class='modal fade bs-example-modal-lg' id='dataDetail' tabindex='-1' role='dialog' data-row='"+i+"' style='overflow:auto;overflow-y:scroll;'>                                                               "+
            "    <div class='modal-dialog modal-lg'>                                                                                                                 "+
            "        <div class='modal-content'>                                                                                                                     "+
            "            <div class='modal-header'>                                                                                                                  "+
            "                <button type='button' class='close' data-dismiss='modal'>&times;</button>                                                               "+
            "                <h4 class='modal-title' id='dinging_lable'>"+dialogTitle+"</h4>                                                                                "+
            "            </div>                                                                                                                                      "+
            "            <div class='modal-body'>                                                                                                                    "+
            "                <div class='dinging-detailed'>                                                                                                          "+
            "                    <table class='table'>                                                                                                               "+
            trs;
//
//
//
//            "                        <tr>                                                                                                                            "+
//            "                            <td>编号</td>                                                                                                               "+
//            "                            <td data-value=''></td>                                                                                                    "+
//            "                        </tr>                                                                                                                           "+
//

             if(operations.length>0){
            htmlStr+="                        <tr class='visible-xs "+trClass+"' style='"+trStyle+"'>                                                                                                         "+
            "                            <td>操作</td>  <td>";

                                    for(o=0;o<operations.length;o++){
                                        var operation=operations[o];
                                        htmlStr+="						<a href='#' name='"+operation.name+"' data-row='"+i+"' operation-id='"+o+"'>"+operation.text+"</a>";
                                    }

//            "                            <td><a href='#' name='edit' data-row='"+i+"'>编辑</a><a href='#' name='remove' data-row='"+i+"'>删除</a></td>                                               "+

            htmlStr+="                        </td></tr>                                                                                                                           ";
             }

        htmlStr+="                        <tr>                                                                                                                            "+
            "                            <td colspan='2'><button type='button' class='btn btn-primary' data-dismiss='modal' aria-hidden='true'>确定</button></td>    "+
            "                        </tr>                                                                                                                           "+
            "                    </table>                                                                                                                            "+
            "                </div>                                                                                                                                  "+
            "            </div>                                                                                                                                      "+
            "        </div>                                                                                                                                          "+
            "    </div>                                                                                                                                              "+
            "</div>                                                                                                                                                  ";

        var dialog = $(htmlStr);
        $("body").append(htmlStr);
        //添加事件处理
        for(o=0;o<operations.length;o++){
            var operation=operations[o];
            //alert(operation.name);
            $("a[name='"+operation.name+"']").unbind("click");
            $("a[name='"+operation.name+"']").click(function(){
                var rowId=$(this).attr("data-row");
                var operationId=$(this).attr("operation-id");
                //alert("rowId->"+rowId)
                var row=rows[rowId];
                var preRow=null;
                if(parseInt(rowId)>0){
                    preRow=rows[parseInt(rowId)-1];
                }
                var nextRow=null;
                if(parseInt(rowId)<rows.length-1){
                    nextRow=rows[parseInt(rowId)+1];
                }
                //alert(operationId);
                operations[operationId].handler(row,preRow,nextRow,rowId);
            });
        }
        scrollTopTemp=document.body.scrollTop;
        //console.info("scrollTop"+scrollTopTemp);
        $("#dataDetail").modal('show');
        $("#dataDetail").unbind("hide.bs.modal");
        $("#dataDetail").on("hide.bs.modal",function(){
            document.body.scrollTop=scrollTopTemp;
        });
    }

    function render(){
        if(rows==null||rows.length==0){
            var htmlStr="<div style=\"margin: 15px 0px; text-align: center;\">"+
                        "    没有数据！"+
                        "</div>";
            dataDiv.html(htmlStr);
        }else{
            //alert(fields.length);
            var theadTd="";
            for(i=0;i<fields.length;i++){
                var field=fields[i];
                //alert("field.text"+field.text);
                if(field.isParam){continue;}
                if(field.inTable){
                    if(field.autoHidden){
                        theadTd+="<td class='hidden-xs'>"+field.text+"</td>";
                    }else{
                        theadTd+="<td>"+field.text+"</td>";
                    }
                }
            }
            //alert("theadTd"+theadTd);
            var dataTr1="";
            for(i=0;i<rows.length;i++){
                var row=rows[i];
                var dataTr="";
                var rowParams={};
                var trClass="";
                var trStyle="";
                var bcColor="";
                var bcWidth="";
                for(j=0;j<fields.length;j++){
                    var tdClass="";
                    var tdStyle="";
                    var field=fields[j];
                    //alert("field.name->"+field.name);
                    var value=row[field.name];
                    //alert("value->"+value);
                    if(field.isBcColor){
                        var bcColor=field.text;
                        if(typeof(field.render)!='undefined'){
                            var index=i;
                            if(offset>-1){
                                index+=offset;
                            }
                            bcColor=field.render(row,index,rowParams);
                        }
                        continue;
                    }
                    if(field.isBcWidth){
                        var bcWidth=field.text;
                        if(typeof(field.render)!='undefined'){
                            var index=i;
                            if(offset>-1){
                                index+=offset;
                            }
                            bcWidth=field.render(row,index,rowParams);
                        }
                        continue;
                    }
                    if(field.isParam){
                        var param=field.text;
                        if(typeof(field.render)!='undefined'){
                            var index=i;
                            if(offset>-1){
                                index+=offset;
                            }
                            param=field.render(row,index,rowParams);
                        }
                        rowParams[field.name]=param;
                        continue;
                    }
                    if(field.isTrClass){
                        trClass=field.text;
                        if(typeof(field.render)!='undefined'){
                            var index=i;
                            if(offset>-1){
                                index+=offset;
                            }
                            trClass=field.render(row,index,rowParams);
                        }
                        continue;
                    }
                    if(field.isTrStyle){
                        trStyle=field.text;
                        if(typeof(field.render)!='undefined'){
                            var index=i;
                            if(offset>-1){
                                index+=offset;
                            }
                            trStyle=field.render(row,index,rowParams);
                        }
                        continue;
                    }
                    if(field.inTable){
                        if(typeof(field.render)!='undefined'){
                            var index=i;
                            if(offset>-1){
                                index+=offset;
                            }
                            value=field.render(row,index,rowParams);
                        }
                        if(value == null){
                            value = "";
                        }
                        if(field.autoHidden){
                            dataTr+="<td class='hidden-xs "+tdClass+"' style='"+tdStyle+"'>"+value+"</td>";
                        }else{
                            dataTr+="<td class='"+tdClass+"' style='"+tdStyle+"'>"+value+"</td>";
                        }
                    }
                }
                if(showDetail){
                    dataTr+="        <td class='hidden-xs' style=''>";
                }else{
//                    dataTr+="        <td class=''>";
                }
                if(showDetail){
                    dataTr+="						<a href='#' name='detail' data-row='"+i+"'>查看</a></td>";
                }

                if(showDetail){
                    dataTr+="        <td class='visible-xs' style=''>";
                    dataTr+="						<a class='dinging-examine' href='#' name='detail' data-row='"+i+"'>查看详细</a>";
                    dataTr+="				</td>";
                }
                if(showDetail) {
                    if (operations.length > 0) {
                        dataTr += "<td class='hidden-xs' style=''>";
                        for (o = 0; o < operations.length; o++) {
                            var operation = operations[o];
                            dataTr += "						<a href='#' name='" + operation.name + "' data-row='" + i + "' operation-id='" + o + "'>" + operation.text + "</a>";
                        }
                        dataTr += "				</td>";
                    }
                }else{
                    if (operations.length > 0) {
                        dataTr += "<td class='' style=''>";
                        for (o = 0; o < operations.length; o++) {
                            var operation = operations[o];
                            dataTr += "						<a href='#' name='" + operation.name + "' data-row='" + i + "' operation-id='" + o + "'>" + operation.text + "</a>";
                        }
                        dataTr += "				</td>";
                    }
                }
                dataTr+="    </tr>";
                dataTr1+="<tr class='"+trClass+"' style='"+trStyle+"'>"+dataTr;
                dataTr1+="<tr class=\"tr-color-line\">                                                       "+
                "    <td colspan=\""+fields.length+"\" style=\"border:0;padding: 0\">                                 "+
                "        <div style=\"width: 100%;height: auto;margin-top: 0px;\">                "+
                "            <div style=\"width: "+bcWidth+";height: 0px;background-color:"+bcColor+"\">         "+
                "            </div>                                                               "+
                "        </div>                                                                   "+
                "    </td>                                                                        "+
                "</tr>";
            }
            //alert("dataTr->"+dataTr);
            var tableHtmlStr="<table class='table'>"+
                "    <thead>"+
                "        <tr>"+
                theadTd;
            if(showDetail){
                tableHtmlStr+="            <td class='hidden-xs'>查看</td>";
                if(operations.length>0){
                tableHtmlStr+="            <td class='hidden-xs'>操作</td>";
                }
            }else{
//                tableHtmlStr+="            <td class=''>查看</td>";
                if(operations.length>0){
                    tableHtmlStr+="            <td class=''>操作</td>";
                }
            }
            if(showDetail){
                tableHtmlStr+="            <td class='visible-xs'>详细</td>";
//                tableHtmlStr+="            <td class='visible-xs'>操作</td>";
            }
            tableHtmlStr+="        </tr>"+
                "    </thead>"+
                "    <tbody>"+
                dataTr1+
                "    </tbody>"+
            "</table>";
            var pageHtmlStr="<div class='right-table-pagination'>"+
                "<nav>"+
                    "   <ul class='pager'> "+
                    "           <li class='previous'><a href='#' name='prev'><i class='glyphicon glyphicon-arrow-left'></i>上一页</a></li>"+
                    "       <span class='page'>"+
                    "<select id='pagechanger'>" ;
                    for(p=1;p<=pages;p++){
                        if(p==currentPage){
                            pageHtmlStr+="<option value='"+p+"' selected>"+p+"</option>" ;
                        }else{
                            pageHtmlStr+="<option value='"+p+"'>"+p+"</option>" ;
                        }
                    }
                    pageHtmlStr+= "</select>"+
                "/"+pages+"页<i class='hidden-xs'>，共"+results+"条</i></span>"+
                    "       <li class='next'><a href='#' name='next'>下一页<i class='glyphicon glyphicon-arrow-right'></i></a></li>"+
                    "   </ul>"+
                "</nav>"+
            "</div>";
            //alert("tableHtmlStr->"+tableHtmlStr);
            var ts="<div style=\"line-height:30px;height:30px;color:red;border:1px solid #ddd;padding-left:20px;\">在下面灰色区域，点击右键试试</div>";
            if(!showContextMenuTip){
                ts="";
            }
            var htmlStr=ts+tableHtmlStr+"";
            if(showPages){
                htmlStr+=pageHtmlStr;
            }
            dataDiv.html(htmlStr);

            //设置背景色相关
            $(".tr-color-line").each(function(){
                $(this).find("div").css("margin-top",-$(this).prev().height());
                $(this).find("div").find("div").css("height",$(this).prev().height());
            });
            $(window).unbind("resize");
            $(window).resize(function(){
                $(".tr-color-line").each(function(){
                    $(this).find("div").css("margin-top",-$(this).prev().height());
                    $(this).find("div").find("div").css("height",$(this).prev().height());
                });
            });


            showFields=new Array();
            //添加右击菜单
            var contextMenuItems={};
            for(i=0;i<fields.length;i++){
                var field=fields[i];
                if(field.isBcColor||field.isBcWidth||field.isParam||field.isTrClass||field.isTrStyle){
                    continue;
                }
                if(field.inTable) {
                    contextMenuItems[field.name]={name: field.text, icon: "hook"};
                    showFields.push(field.name);
                }else{
                    contextMenuItems[field.name]= {name: field.text, icon: ""};
                }
            }
            if(typeof (reportShowFields)!="undefined"){
                reportShowFields(showFields);
            }
            $.contextMenu("destroy",{selector: 'table>thead'});
            $.contextMenu('create',{
                selector: 'table>thead',
                callback: function(key, options) {
                    for(i=0;i<fields.length;i++){
                        var field=fields[i];
                        if(field.isBcColor||field.isBcWidth||field.isParam||field.isTrClass||field.isTrStyle){
                            continue;
                        }
                        if(field.name==key) {
                            field.inTable=!field.inTable;
                            render();
                            return true;
                        }
                    }
                },
                items:  contextMenuItems
            });


            //绑定点击事件
            $("#pagechanger").change(function(){
                var selectPage=$(this).val();
                rload(selectPage);
            });
            $("a[name='prev']").click(function(){
                if(currentPage>1){
                    rload(currentPage-1);
                }
            });
            if(currentPage<=1){
                $("a[name='prev']").parent().addClass("disabled");
            }
            $("a[name='next']").click(function(){
                if(currentPage<pages){
                    rload(currentPage+1);
                }
            });
            if(currentPage>=pages){
                $("a[name='next']").parent().addClass("disabled");
            }

            $("a[name='detail']").click(function(){
                var rowId=$(this).attr("data-row");
                //alert("rowId->"+rowId)
                var row=rows[rowId];
                renderDialog(rowId);
                //alert("row->"+row);
//                var trs=$("#"+dialogId).find("table").find("tr");
//                //alert("trs->"+trs);
//                trs.each(function(){
//                    var td=$(this).find("td[fieldName]");
//                    var fieldName=td.attr("fieldName");
//                    //alert("fieldName->"+fieldName);
//                    var value=row[fieldName];
//                    td.attr("data-value",value);
//                    td.text(value);
//                    //alert("value->"+value);
//                });
//                //alert("dd");
//                $("#"+dialogId).modal('show');
            });

            for(o=0;o<operations.length;o++){
                var operation=operations[o];
                //alert(operation.name);
                $("a[name='"+operation.name+"']").unbind("click");
                $("a[name='"+operation.name+"']").click(function(){
                    var rowId=$(this).attr("data-row");
                    var operationId=$(this).attr("operation-id");
                    //alert("rowId->"+rowId)
                    var row=rows[rowId];var preRow=null;
                    if(parseInt(rowId)>0){
                        preRow=rows[parseInt(rowId)-1];
                    }
                    var nextRow=null;
                    if(parseInt(rowId)<rows.length-1){
                        nextRow=rows[parseInt(rowId)+1];
                    }
                    //alert(operationId);
                    operations[operationId].handler(row,preRow,nextRow,rowId);
                });
            }
        }
        if(typeof(afterRenderEach)!="undefined" ){
            afterRenderEach();
        }
    }

    function blockRender(){
        if(rows==null||rows.length==0){
            var htmlStr="<div style=\"margin: 15px 0px; text-align: center;\">"+
                "    没有数据！"+
                "</div>";
            dataDiv.html(htmlStr);
        }else{
            //alert(fields.length);
            var dataTr="";
            var preRow=null;
            for(i=0;i<rows.length;i++){
                var row=rows[i];
                dataTr+="<div class=\"col-lg-3 col-md-3 col-sm-4 col-xs-6\">"+
                    "<div style=\"border: solid 1px #b8b8b8;padding: 5px;margin: 5px 0px;\">";
                for(j=0;j<fields.length;j++){
                    var field=fields[j];
                    //alert("field.name->"+field.name);
                    var value=row[field.name];
                    //alert("value->"+value);
                    if(field.inTable){
                        if(typeof(field.render)!='undefined'){
                            var index=i;
                            if(offset>-1){
                                index+=offset;
                            }
                            value=field.render(row,index,preRow);
                        }
                        if(value == null){
                            value = "";
                        }
                        if(field.autoHidden){
                            dataTr+=""+value+"";
                        }else{
                            dataTr+=""+value+"";
                        }
                    }
                }
                dataTr+="</div></div>";
                preRow=row;
            }
            //alert("dataTr->"+dataTr);

            var tableHtmlStr="<div class=\"row screen-group\">";

            tableHtmlStr+=dataTr;

            tableHtmlStr+="</div>";

            var pageHtmlStr="<div class='right-table-pagination'>"+
                "<nav>"+
                "   <ul class='pager'> "+
                "           <li class='previous'><a href='#' name='prev'><i class='glyphicon glyphicon-arrow-left'></i>上一页</a></li>"+
                "       <span class='page'>"+
                "<select id='pagechanger'>" ;
            for(p=1;p<=pages;p++){
                if(p==currentPage){
                    pageHtmlStr+="<option value='"+p+"' selected>"+p+"</option>" ;
                }else{
                    pageHtmlStr+="<option value='"+p+"'>"+p+"</option>" ;
                }
            }
            pageHtmlStr+= "</select>"+
                "/"+pages+"页<i class='hidden-xs'>，共"+results+"条</i></span>"+
                "       <li class='next'><a href='#' name='next'>下一页<i class='glyphicon glyphicon-arrow-right'></i></a></li>"+
                "   </ul>"+
                "</nav>"+
                "</div>";
            //alert("tableHtmlStr->"+tableHtmlStr);
            var htmlStr=tableHtmlStr+"";
            if(showPages){
                htmlStr+=pageHtmlStr;
            }
            dataDiv.html(htmlStr);

            //绑定点击事件
            $("#pagechanger").change(function(){
                var selectPage=$(this).val();
                rload(selectPage);
            });
            $("a[name='prev']").click(function(){
                if(currentPage>1){
                    rload(currentPage-1);
                }
            });
            if(currentPage<=1){
                $("a[name='prev']").parent().addClass("disabled");
            }
            $("a[name='next']").click(function(){
                if(currentPage<pages){
                    rload(currentPage+1);
                }
            });
            if(currentPage>=pages){
                $("a[name='next']").parent().addClass("disabled");
            }

            $("a[name='detail']").click(function(){
                var rowId=$(this).attr("data-row");
                //alert("rowId->"+rowId)
                var row=rows[rowId];
                renderDialog(rowId);
                //alert("row->"+row);
//                var trs=$("#"+dialogId).find("table").find("tr");
//                //alert("trs->"+trs);
//                trs.each(function(){
//                    var td=$(this).find("td[fieldName]");
//                    var fieldName=td.attr("fieldName");
//                    //alert("fieldName->"+fieldName);
//                    var value=row[fieldName];
//                    td.attr("data-value",value);
//                    td.text(value);
//                    //alert("value->"+value);
//                });
//                //alert("dd");
//                $("#"+dialogId).modal('show');
            });

            for(o=0;o<operations.length;o++){
                var operation=operations[o];
                //alert(operation.name);
                $("a[name='"+operation.name+"']").click(function(){
                    var rowId=$(this).attr("data-row");
                    var operationId=$(this).attr("operation-id");
                    //alert("rowId->"+rowId)
                    var row=rows[rowId];
                    var preRow=null;
                    if(parseInt(rowId)>0){
                        preRow=rows[parseInt(rowId)-1];
                    }
                    var nextRow=null;
                    if(parseInt(rowId)<rows.length-1){
                        nextRow=rows[parseInt(rowId)+1];
                    }
                    //alert(operationId);
                    operations[operationId].handler(row,preRow,nextRow,rowId);
                });
            }
        }
        if(typeof(afterRenderEach)!="undefined" ){
            afterRenderEach();
        }
    }
    function rload(page){
        data["page"]=page;
        if(typeof(webContext)!='undefined'){
            $.doSubmit({
                type:type,
                submitUrl:url,
                data:data,
                async:async,
                traditional:traditional,
                showwait:showwait,
                callBack:function(response){
                    var redata=$.parseJSON(response);
                    currentPage=redata.currentPage;
                    pages=redata.pages;
                    results=redata.results;
                    rows=redata.rows;
                    offset=redata.offset;
                    max=redata.max;
                    if(typeof(beforeRender) != 'undefined' ){
                        beforeRender(redata);
                    }
                    if(renderBlock){
                        blockRender();
                    }else{
                        render();
                    }
                    if (typeof($("#dataDetail")) != 'undefined') {
                        if($("#dataDetail").css("diplay")=="block"){
                            var i=$("#dataDetail").attr("data-row");
                            renderDialog(i);
                        }
                    }

                    if(typeof(afterLoad)!="undefined" ){
                        afterLoad();
                    }
                    if(typeof(loadSum) != 'undefined' ){
                        loadSum(redata);
                    }
                },
                webContext:webContext
            });
        }else{
            $.ajax({
                type: type,
                url: url,
                data: data,
                async:async,
                traditional:traditional,
                showwait:showwait,
                success:function(response){
                    if(typeof(response)!="object"&&response.indexOf("\<!DOCTYPE html\>")>0){
                        window.location.reload();
                        //return;
                    }
                    //alert(response);
                    var redata=$.parseJSON(response);
                    currentPage=redata.currentPage;
                    pages=redata.pages;
                    results=redata.results;
                    rows=redata.rows;
                    offset=redata.offset;
                    max=redata.max;
                    if(typeof(beforeRender) != 'undefined' ){
                        beforeRender(redata);
                    }
                    if(typeof(loadSum) != 'undefined' ){
                        loadSum(redata);
                    }
                    if(typeof(afterLoad)!="undefined" ){
                        afterLoad();
                    }
                },
                complete: function(e, xhr, settings){
                    //alert(e.status);
                    if(e.status === 200){
                        render();
                        if (typeof($("#dataDetail")) != 'undefined') {
                            if($("#dataDetail").css("diplay")=="block"){
                                 var i=$("#dataDetail").attr("data-row");
                                renderDialog(i);
                            }
                        }
                    }else if(e.status === 304){

                    }else{

                    }
                }
            });
        }
    }
    function load(){
        rload(1);
    }



    $.fn.datagridExpandBc=function(method,options){
        if(method=="getRow"){
            var rowIndex=options.index;
            var row=null;
            if(typeof(rows)!="undefined"&&typeof(rowIndex)!="undefined"){
                row=rows[rowIndex];
            }
            return row;
        }
        if(method=="setRow"){
            var rowIndex=options.index;
            var row=options.row;
            if(typeof(rows)!="undefined"&&typeof(rowIndex)!="undefined"){
                rows[rowIndex]=row;
            }
            return;
        }
        if(method=="render"){
            if(renderBlock){
                blockRender();
            }else{
                render();
            }
            return;
        }

        if(typeof(options)!="undefined") {
            if(typeof(options.data) != 'undefined') {
                data = options.data;
            }
            if(typeof(options.url) != 'undefined') {
                url = options.url;
            }
            if(typeof(options.fields) != 'undefined') {
                fields = options.fields;
            }
            if(typeof(options.operations) != 'undefined') {
                operations = options.operations;
            }
            if (typeof(options.dialogTitle) != 'undefined') {
                dialogTitle = options.dialogTitle;
            }
            if (typeof(options.showwait) != 'undefined') {
                showwait = options.showwait;
            }
            if (typeof(options.showDetail) != 'undefined') {
                showDetail = options.showDetail;
            }
            if(typeof(options.beforeRender) != 'undefined'){
                beforeRender = options.beforeRender;
            }
            if(typeof(options.webContext) != 'undefined') {
                webContext = options.webContext;
            }
            if (typeof(options.webContext) != 'undefined') {
                if (webContext.lastIndexOf("/") != webContext.length - 1) {
                    webContext += "/";
                }
            }
            if (typeof(options.async) != "undefined") {
                async = options.async;
            }
            if(typeof(options.afterLoad) != 'undefined') {
                afterLoad = options.afterLoad;
            }
            if(typeof(options.loadSum) != 'undefined') {
                loadSum = options.loadSum;
            }

            if(typeof(options.traditional) != "undefined"){
                traditional = options.traditional;
            }
            if(typeof(options.renderBlock) !="undefined"){
                renderBlock=options.renderBlock;
            }
            if(typeof(options.reportShowFields) !="undefined" ){
                reportShowFields=options.reportShowFields;
            }
            if(typeof(options.type) !="undefined" ){
                type=options.type;
            }
            if(typeof(options.afterRenderEach) !="undefined" ){
                afterRenderEach=options.afterRenderEach;
            }
            if(typeof(options.showPages) !="undefined" ){
                showPages=options.showPages;
            }
            if(typeof(options.showContextMenuTip) !="undefined" ){
                showContextMenuTip=options.showContextMenuTip;
            }

        }
        return this.each(function () {
             dataDiv=$(this);
             if(method=="load"){
                load();
             }else if(method=="reload"){
                 rload(currentPage);
             }
        });
    }

})(jQuery);
